﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading.Tasks;

#endregion


namespace ESF.General
{
	public static class MassiveEmail
	{
		public static void SendEmail<O>(IEnumerable<O> pDataSource, string pSubject, string pBody, Func<O, string> pToGetter, bool pIsHTML = false, bool pCopyToLog = false, bool pIgnoreBadEmailTo = true)
		{
			SendEmail<O>(pDataSource, delegate(O pRecord) { return pSubject; }, delegate(O pRecord) { return pBody; }, pToGetter, pIsHTML, pCopyToLog, pIgnoreBadEmailTo);
		}


		public static void SendEmail<O>(IEnumerable<O> pDataSource, string pSubject, Func<O, string> pBodyMaker, Func<O, string> pToGetter, bool pIsHTML = false, bool pCopyToLog = false, bool pIgnoreBadEmailTo = true)
		{
			SendEmail<O>(pDataSource, delegate(O pRecord) { return pSubject; }, pBodyMaker, pToGetter, pIsHTML, pCopyToLog, pIgnoreBadEmailTo);
		}


		public static void SendEmail<O>(IEnumerable<O> pDataSource, Func<O, string> pSubjectMaker, Func<O, string> pBodyMaker, Func<O, string> pToGetter, bool pIsHTML = false, bool pCopyToLog = false, bool pIgnoreBadEmailTo = true)
		{
			Parallel.ForEach<O> (pDataSource, 
				delegate (O pRecord)
				{
					EmailSender.Send(pSubjectMaker (pRecord), pBodyMaker (pRecord), pIsHTML, pCopyToLog, pIgnoreBadEmailTo, pToGetter (pRecord));
				});
		}
	}
}
